import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import {
    getCrawlerList,
    createCrawlerTask,
    getCrawlerTaskDetail,
    getArticleList,
    getArticleDetail,
    getStatistics,
    deleteArticle, batchDelete, deleteTask, batchDeleteTask
} from '@/api/crawler'

export const useCrawlerStore = defineStore('crawler', () => {
    // 状态
    const tasks = ref([])
    const articles = ref([])
    const statistics = ref(null)
    const loading = ref(false)
    const currentTask = ref(null)
    const currentArticle = ref(null)

    // 任务相关方法
    const fetchTasks = async (params) => {
        try {
            loading.value = true
            const { data } = await getCrawlerList(params)
            tasks.value = data
            return data
        } finally {
            loading.value = false
        }
    }

    const createTask = async (taskData) => {
        try {
            loading.value = true
            const { data } = await createCrawlerTask(taskData)
            await fetchTasks()
            return data
        } finally {
            loading.value = false
        }
    }

    const getTaskDetail = async (taskId) => {
        try {
            loading.value = true
            const { data } = await getCrawlerTaskDetail(taskId)
            currentTask.value = data
            return data
        } finally {
            loading.value = false
        }
    }

    // 文章相关方法
    const fetchArticles = async (params) => {
        try {
            loading.value = true
            const { data } = await getArticleList(params)
            articles.value = data
            return data
        } finally {
            loading.value = false
        }
    }

    const fetchArticleDetail = async (id) => {
        try {
            loading.value = true
            const { data } = await getArticleDetail(id)
            currentArticle.value = data
            return data
        } finally {
            loading.value = false
        }
    }

    // 删除单个文章
    const deleteArticleById = async (id) => {
        try {
            await deleteArticle(id)
        } catch (error) {
            console.error('Failed to delete article:', error)
            throw error
        }
    }

    // 批量删除文章
    const batchDeleteArticles = async (ids) => {
        try {
            await batchDelete(ids)
        } catch (error) {
            console.error('Failed to batch delete articles:', error)
            throw error
        }
    }

    // 统计相关方法
    const fetchStatistics = async () => {
        try {
            const { data } = await getStatistics()
            statistics.value = data
            return data
        } catch (error) {
            console.error('Failed to fetch statistics:', error)
        }
    }

    // 删除单个任务
    const deleteTaskById = async (taskId) => {
        try {
            await deleteTask(taskId)
        } catch (error) {
            console.error('Failed to delete task:', error)
            throw error
        }
    }

    // 批量删除任务
    const batchDeleteTasks = async (taskIds) => {
        try {
            await batchDeleteTask(taskIds)
        } catch (error) {
            console.error('Failed to batch delete tasks:', error)
            throw error
        }
    }

    // 计算属性
    const taskCount = computed(() => tasks.value?.total || 0)
    const articleCount = computed(() => articles.value?.total || 0)

    return {
        // 状态
        tasks,
        articles,
        statistics,
        loading,
        currentTask,
        currentArticle,

        // 方法
        fetchTasks,
        createTask,
        getTaskDetail,
        fetchArticles,
        fetchArticleDetail,
        fetchStatistics,
        deleteArticleById,
        batchDeleteArticles,
        deleteTaskById,
        batchDeleteTasks,

        // 计算属性
        taskCount,
        articleCount
    }
})
